<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false"  :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'"
             width="500px">
    <el-form ref="form" :model="form" :rules="rules"  label-width="80px">
      <el-form-item label="分类名称" prop="label">
        <el-input v-model="form.label" maxlength="20" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="是否展示" class="dd-label" prop="status">
        <el-select v-model="form.isShow" style="width: 370px" filterable placeholder="请选择">
          <el-option label="是" :value="1"></el-option>
          <el-option label="否" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上级类目">
        <treeselect v-model="form.pid" :options="types" class="treeselect-main" style="width: 370px" placeholder="选择上级类目"/>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model.number="form.sort" style="width: 185px" :min="0" :max="999" controls-position="right" placeholder="序号"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { typeTree, add } from '@/api/business/courseType'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    components: { Treeselect },
    props: {
      isAdd: {
        type: Boolean,
        required: true
      }
    },
    data() {
      return {
        loading: false, dialog: false, types: [],
        form: {
          id: null,
          label: '',
          isShow: 1,
          pid: '0',
          sort: 0
        },
        rules: {
          label: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      cancel() {
        this.dialog = false
        this.form = {
          id: null,
          label: '',
          isShow: 1,
          pid: '0',
          sort: 0
        }
      },
      doSubmit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.doAdd()
          }
        })
      },
      doAdd() {
        add(this.form).then(res => {
          this.$notify({
            title: '操作成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
          this.cancel()
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
      },
      getCourseType() {
        typeTree().then(data => {
          this.types = []
          const type = { id: '0', label: '课程分类', children: [] }
          type.children = data
          this.types.push(type)
        })
      }
    }
  }
</script>

<style scoped>
  .treeselect-main {
    margin-top: 1px;
    line-height: 36px;
    .vue-treeselect__placeholder {
      line-height: 36px;
    }
    .vue-treeselect__control {
      height: 36px;
    }
  }
</style>
